package com.asiainfo.sqlextractor.exec;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class DataSetHolder {

    private final static ThreadLocal<JSONObject> DATA = new ThreadLocal<>();

    public static final JSONArray EMPTY_JSON_ARRAY = new JSONArray();

    public static void set(JSONObject data) {
        DATA.set(data);
    }

    public static JSONArray get(String tableName) {
        JSONObject data = DATA.get();
        if (data == null) {
            return EMPTY_JSON_ARRAY;
        }
        Object tableData = data.get(tableName);
        if (tableData instanceof JSONArray) {
            return (JSONArray) tableData;
        }
        if (tableData instanceof JSONObject) {
            return new JSONArray().fluentAdd(tableData);
        }
        return EMPTY_JSON_ARRAY;
    }


    public static void remove() {
        DATA.remove();
    }

}
